package br.srv.full.virtual.engine.persistence;

import java.util.Map;

import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceUnit;

import br.srv.full.persistence.BeansManagerException;
import br.srv.full.persistence.UpdateBeansException;
import br.srv.full.persistence.PersistBeansException;
import br.srv.full.persistence.RefreshBeansException;
import br.srv.full.persistence.impl.BeansManagerImpl;
import br.srv.full.virtual.bean.base.QueryID;

/**
 * Acho que esta classe generica está ficando obsoleta ou deve ser revista em um
 * formato que permita mudar o UnitWork de forma mais generica. por exemplo
 * estarei em breve desenvolvimento o sistema de cobrança e este estará usando
 * um UnitWork diferente entáo náo será possivel usar esta classe, já que esta
 * somente aceita o virtualUW
 * 
 * @author Delfino
 * 
 */
@Stateless(name = "BeansManager")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class BeansManagerEJB3 extends BeansManagerImpl implements BeansManagerLocal {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7256862544184157977L;
	private EntityManager em;

	@PersistenceUnit(unitName = "VirtualUW")
	protected void setEmf(EntityManagerFactory p_emf) {
		emf = p_emf;
	}

	@PersistenceContext(unitName = "VirtualUW", type = PersistenceContextType.TRANSACTION)
	protected void setEm(EntityManager p_em) {
		em = p_em;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#createEntityManager(boolean[])
	 */
	@Override
	protected EntityManager createEntityManager(boolean... p_tx) {
		return em;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#rollBackTransaction(javax.persistence.EntityManager)
	 */
	@Override
	protected void rollBackTransaction(EntityManager p_em) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#closeEntityManager(javax.persistence.EntityManager,
	 *      boolean[])
	 */
	@Override
	protected void closeEntityManager(EntityManager p_l_em, boolean... p_tx) {
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean> Object find(Class<bean> p_class) {
		return super.find(p_class);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#find(java.lang.Class,
	 *      java.lang.Object)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, key> bean find(Class<bean> p_class, key p__key) {
		return super.find(p_class, p__key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#find(java.lang.Class,
	 *      br.srv.full.virtual.bean.base.QueryID, java.util.Map)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, params> Object find(Class<bean> p_class, QueryID p_query, Map<String, Object> p_params) {
		// TODO Auto-generated method stub
		return super.find(p_class, p_query, p_params);
	}

	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, params> Object find(Class<bean> _beanClass, String _query, params... _params) {
		return super.find(_beanClass, _query, _params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#find(java.lang.Class,
	 *      br.srv.full.virtual.bean.base.QueryID, params[])
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, params> Object find(Class<bean> p_class, QueryID p__query, params... p__params) {
		return super.find(p_class, p__query, p__params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#find(java.lang.Class,
	 *      br.srv.full.virtual.bean.base.QueryID)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, params> Object find(Class<bean> p_class, QueryID p__query) {
		return super.find(p_class, p__query);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#persist(java.lang.Object,
	 *      java.lang.String[])
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean persist(bean p_bean, String... p_properties) throws BeansManagerException {
		return super.persist(p_bean, p_properties);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#persist(java.lang.Object)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean persist(bean p__bean) throws PersistBeansException {
		return super.persist(p__bean);
	}

	/**
	 * Atualiza o Bean com os dados do banco
	 * 
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#refresh(java.lang.Object,
	 *      java.lang.String[])
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean refresh(bean p_bean, String... p_properties) throws RefreshBeansException {
		return super.refresh(p_bean, p_properties);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#update(java.lang.Object)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean update(bean p_bean) throws UpdateBeansException {
		return super.update(p_bean);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#update(java.lang.Object,
	 *      java.lang.String[])
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean update(bean p_bean, String... p_properties) throws UpdateBeansException {
		return super.update(p_bean, p_properties);
	}

	/**
	 * Atualiza o Bean com os dados do banco.
	 * 
	 * @see br.srv.full.persistence.impl.BeansManagerImpl#refresh(java.lang.Object)
	 */
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public <bean> bean refresh(bean p_bean) throws RefreshBeansException {

		return super.refresh(p_bean);
	}

	/**
	 * pesquisa o bean do tipo informado no banco.
	 */
	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
	public <bean, params> Object find(Class<bean> _beanClass, QueryID _query, String[] p_fieldName, params[] p_params) {
		return super.find(_beanClass, _query, p_fieldName, p_params);
	}
}
